<?php
class Admin_UserController extends Zend_Controller_Action
{
   public function init()
    {
        /* Initialize action controller here */
    	$this->_helper->acl->allow('admin');
    }
    public function indexAction()
    {
    	$message = $this->_helper->FlashMessenger->getMessages();
    	if($message){
    		$this->view->message = $message[0];
    	}
		$this->view->title = 'View all users';
			$this->view->headTitle($this->view->title, 'PREPEND');
		$user = new Application_Model_User();
		$this->view->users = $user->getAll();
    }
	public function editAction(){
		$this->view->title = 'Edit user';
		$this->view->headTitle($this->view->title, 'PREPEND');
		if($input = $this->_checkInput('id', 'user')){
			$user = new Application_Model_User($input->id);
			$form = new Application_Form_User('edit');
			$form->populate($user->getRow());
			$form->excludeFrom('name', 'Db_NoRecordExists', 'name', $user->name);
			$form->excludeFrom('email', 'Db_NoRecordExists', 'email', $user->email);
			$this->view->form = $form;
			$request = $this->getRequest();
			if($request->isPost()){
				if($form->isValid($request->getPost())){
					$user->fill($form->getValues());					
					$user->save();
			    	$messenger = $this->_helper->FlashMessenger;
					$messenger->addMessage('User edited');
					$this->_helper->redirector(null, 'user', 'admin');
				}
			}
		}
		else{
			throw new Zend_Controller_Action_Exception('Invalid input');
		}
	}
	public function deleteAction(){
		$this->_helper->viewRenderer->setNoRender();
		if($input = $this->_checkInput('id', 'user')){
			$user = new Application_Model_User($input->id);
			$user->delete();
			$messenger = $this->_helper->FlashMessenger;
			$messenger->addMessage('User deleted');
			$this->_helper->redirector(null, 'user', 'admin');
		}
		else{
			throw new Zend_Controller_Action_Exception('Invalid input');
		}
	}
	public function addAction(){
		$this->view->title = 'Add new user';
		$this->view->headTitle($this->view->title, 'PREPEND');
		$form = new Application_Form_User();
		$request = $this->getRequest();
		if($request->isPost()){
			if($form->isValid($request->getPost())){
				$user = new Application_Model_User();
				$user->fill($form->getValues());				
				$user->save();
			    $messenger = $this->_helper->FlashMessenger;
				$messenger->addMessage('User added');
				$this->_helper->redirector(null, 'user', 'admin');
			}
		}		
		$this->view->form = $form;
	}
	private function _checkInput($param, $field = null){
		$validatorsSet = array();
		$validatorsSet[] = 'Int';
		$validatorsSet[] = new Zend_Validate_GreaterThan(0);
		$validatorsSet[] = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::ALL);
		if($field){
			$validatorsSet[] = new Zend_Validate_Db_RecordExists(array('table' => $field, 'field' => $param));
		}
    	$validators = array($param => $validatorsSet);
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($this->getRequest()->getParams());
	    if($input->isValid()){
	    	return $input;
	    }
	    else{
	    	return false;
	    }		
	}
}